﻿#region Using Region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unisys.InfoImage.IIFCAL;
using Unisys.InfoImage.IIFCAL;
using Unisys.InfoImage.IIFCAL.IIFClient;
using Unisys.InfoImage.IIFCAL.IIFClient.IIFQueue;
using Unisys.InfoImage.IIFCAL.IIFClient.IIFWorkitem;
using Unisys.InfoImage.IIFCAL.IIFClient.IIFWorkitem.IIFWorkitemInformation;
using Unisys.InfoImage.IIFCAL.IIFClient.IIFWorkitem.IIFForm;
using Unisys.InfoImage.IIFCAL.IIFClient.IIFWorkitem.IIFPage;
using Unisys.InfoImage.IIFCAL.IIFClient.IIFQuery;
using Unisys.InfoImage.IIFCAL.IIFWorkflow;
using ICE.Vacations.DataBase;


#endregion


namespace ICE.Vacations.InfoImage
{
    public class IIFWorkItem : IIFConnection
    {
        #region Global Resources

        private IIFCALDocument calDocument;
        private IIFCALFormFields calFormFields;
        private IIFCALIndexFields calIndexFields;
        private IIFCALPage calPage;
        private IIFCALPages calPages;
        private IIFCALQueue calQueue;
        private IIFCALWorkitemInfo calWorkItemInfo;


        #region Workflow Variables

        private IIFCALWorkflowVariableValue aprob_1 = new IIFCALWorkflowVariableValue();
        private IIFCALWorkflowVariableValue aprob_2 = new IIFCALWorkflowVariableValue();
        private IIFCALWorkflowVariableValue aprob_3 = new IIFCALWorkflowVariableValue();
        private IIFCALWorkflowVariableValue totalAprob = new IIFCALWorkflowVariableValue();

        #endregion

        #region DataBase Objects
        private OracleDataClass oracle = new OracleDataClass();
        private SQLDataClass sql = new SQLDataClass();
        #endregion

        #endregion

        public bool CreateWorkItem()
        {
           
            calWorkItemInfo = (IIFCALWorkitemInfo)calClient.CreateDocument("docName", "clase");
            calClientListItem = (IIFCALClientListItem)calClientList.Add(calWorkItemInfo.ObjID, IIFClientListAddConstants.AddRetain);
            calDocument = (IIFCALDocument)calClientListItem.Open
(IIFOpenConstants.OpenReadWrite);
            calFormFields = (IIFCALFormFields)calDocument.FormFields(IIFFieldConstants.FieldsNoViews);

            calFormFields.Find("CEDULA").SetValue(null, "valor");
            calFormFields.Find("DEPEND").SetValue(null, "valor");
            calFormFields.Find("folio").SetValue(null, "valor");
            calFormFields.Find("TIPOO").SetValue(null, "valor");
            calFormFields.Find("CF").SetValue(null, "valor");
            calFormFields.Find("TIPOO").SetValue(null, "valor");
            calFormFields.Find("UDOCUMEN").SetValue(null, "valor");
            calFormFields.Find("FECHA").SetValue(null, "valor");
            calFormFields.Find("AÑO").SetValue(null, "valor");
            calFormFields.Find("FECHA").SetValue(null, "valor");
            calFormFields.Find("JEFE").SetValue(null, "valor");
            calFormFields.Find("FEC_INICIO").SetValue(null, "valor");
            calFormFields.Find("FEC_FIN").SetValue(null, "valor");
            calFormFields.Find("COMENTARIO").SetValue(null, "valor");
            calFormFields.Find("ESTADO_SOLICITUD").SetValue(null, "valor");
            
            calDocument.Save();
            calDocument.Close(IIFCloseConstants.CloseSaveChanges);
            return true;
        }

        public bool CreateWorkitem(string requestID,string employeeID, string employeeName, string folio,string tipoo, string funtionalCode, string unitDoc, string date, string year, string review, string dateFrom, string dateTo, string comments, string state)
        {
            try
            {
                #region Create IIF Document

                calWorkItemInfo = (IIFCALWorkitemInfo)calClient.CreateDocument(requestID, unitDoc);
                calClientListItem = (IIFCALClientListItem)calClientList.Add(calWorkItemInfo.ObjID, IIFClientListAddConstants.AddRetain);
                calDocument = (IIFCALDocument)calClientListItem.Open(IIFOpenConstants.OpenReadWrite);
                calFormFields = (IIFCALFormFields)calDocument.FormFields(IIFFieldConstants.FieldsNoViews);

                #endregion

                #region Set Form Values
                calFormFields.Find("ID_SOLICITUD").SetValue(null, requestID);
                calFormFields.Find("CEDULA").SetValue(null, employeeID);
                calFormFields.Find("DEPEND").SetValue(null, employeeName);
                calFormFields.Find("folio").SetValue(null, folio);
                calFormFields.Find("TIPOO").SetValue(null, tipoo);
                calFormFields.Find("CF").SetValue(null, funtionalCode);
                calFormFields.Find("UDOCUMEN").SetValue(null, unitDoc);
                calFormFields.Find("FECHA").SetValue(null, date);
                calFormFields.Find("AÑO").SetValue(null, year);
                calFormFields.Find("JEFE").SetValue(null, review);
                calFormFields.Find("FEC_INICIO").SetValue(null, dateFrom);
                calFormFields.Find("FEC_FIN").SetValue(null, dateTo);
                calFormFields.Find("COMENTARIO").SetValue(null, comments);
                calFormFields.Find("ESTADO_SOLICITUD").SetValue(null, state);

               
                #endregion

                #region Set WorkflowVariables Values
                totalAprob.Value = sql.TotalAprovees(employeeID);
                aprob_1.Value = 0;
                aprob_2.Value = 0;
                aprob_3.Value = 0;

                #endregion

                #region Save Document - IIF Desktop
                calDocument.Save();
                calDocument.Close(IIFCloseConstants.CloseSaveChanges);
                #endregion

                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        public bool SendToWorkFlow(string queueName)
        {
            try
            {
               // Service.sendLog("Estableciendo el workstep ["+queueName +"] a donde será enviado el workitem...", Service.errorType.Information_Message);
                calQueue.Name = queueName;
                calQueue.Type = IIFQueueTypeConstants.QueueTypeWorkstep;
                //Service.sendLog("Workstep [" + queueName + "] asignado correctamente", Service.errorType.Information_Message);
                
                if(queueName.Equals(""))
                {
                  //  Service.sendLog("No se encontro un nombre de workstep de salida para el documento [" + "] por lo que será enviado a la cola [Default].", Service.errorType.Information_Message);

                    calClientListItem.SendToDefault(IIFSendConstants.SendDiscard);
                    //Service.sendLog("El workitem [" + "] fue enviado a la cola [Default].", Service.errorType.Information_Message);

                }else
                {
                   // Service.sendLog("Enviando workitem [" + "] a la cola ["+queueName+"]...", Service.errorType.Information_Message);

                    calClientListItem.Send(calQueue, IIFSendConstants.SendDiscard);
                    //Service.sendLog("El workitem [" + "] fue enviado a la cola [" + queueName + "].", Service.errorType.Information_Message);

                }
                return true;
            }
            catch (IIFCALException exc)
            {
               // Service.sendLog("No se logró enviar el workitem al flujo de trabajo.", Service.errorType.Error_Message, exc);
                return false;
            }
           
        }

        public bool ReclasifyWorkItem(IIFCALDocument document, string newClassName)
        {
            try
            {
                calDocument = document;

                try
                {
                    calDocument.Reclassify(newClassName, IIFReclassifyConstants.ReclassifyRetainFormData);
                    calDocument.Save();
                    calDocument.Close(IIFCloseConstants.CloseSaveChanges);
                    return true;
                }
                catch (IIFCALException calException)
                {
                    calDocument.Close(IIFCloseConstants.CloseAbortChanges);
                    return false;
                }

            }
            catch (Exception excp)
            {
                return false;
            }
        }

        public bool LoadPDF(string pdfFile) 
        {
            return false;
        }

        public IIFCALDocument Search(string objID) 
        {
            IIFCALIndexFields Object_ID = new IIFCALIndexFields();
            Object_ID.SetValue("OBJECT_ID", objID);

            IIFCALQueryResults result = (IIFCALQueryResults)calClient.Query("", IIFObjTypeConstants.ObjTypeDocument, IIFLocationConstants.LocationDomain & IIFLocationConstants.LocationArchive, 100, Object_ID);

            if (result.Count > 0)
            {
                var gCalEnumItemFound = result.GetItem(1);
                calWorkItemInfo = (IIFCALWorkitemInfo)gCalEnumItemFound.Info;

                var gCalClientListItemFound = (IIFCALClientListItem)gCalEnumItemFound.Retrieve(Unisys.InfoImage.IIFCAL.IIFClient.IIFWorkitem.IIFWorkitemInformation.IIFEnum.IIFRetrieveConstants.RetrieveRetain);

                IIFCALDocument document = (IIFCALDocument)gCalClientListItemFound.Open(IIFOpenConstants.OpenReadWrite | IIFOpenConstants.OpenArchive);
                return document;

            }
            else
            {
                return null;
            }
        }

    }
}
